import loguru
from PySide6.QtGui import QDragEnterEvent
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit, QLabel, QTextEdit

from src.Ui_main import Ui_Form
from src.component.cmd_text_edit import CMDTextEdit
from src.component.drop_mask import DropMask
from src.core.program_text import ABOUT, HELP
from src.core.enums import ProgramsStatus


class MainView(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)

        self.setWindowTitle("DesktopMMD4Unlock")
        self.setAcceptDrops(True)

        self._cmd_text_edit = CMDTextEdit()
        self._cmd_text_edit.redirect_print = True

        # 获取原来的 textEdit 控件的父布局
        layout = self.ui.textEdit.parentWidget().layout()

        # 移除原来的 textEdit 控件
        layout.removeWidget(self.ui.textEdit)
        self.ui.textEdit.deleteLater()

        # 将新的 CMDTextEdit 控件添加到布局中
        layout.addWidget(self._cmd_text_edit)

        self.ui.textEdit = self._cmd_text_edit

        self._help_text_edit = QTextEdit()
        self._help_text_edit.setWindowTitle('帮助')
        self._help_text_edit.resize(400, 300)
        self._help_text_edit.setReadOnly(True)
        self._help_text_edit.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth)

        self._about_text_edit = QTextEdit()
        self._about_text_edit.setWindowTitle('关于')
        self._about_text_edit.resize(400, 300)
        self._about_text_edit.setReadOnly(True)
        self._about_text_edit.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth)

        self._drop_mask = DropMask(self, "拖入文件")
        self._drop_mask.droped_file_url.connect(self._set_file_path)
        self._drop_mask.hide()

        self._connect_signal()

    @property
    def status(self) -> str:
        return self.get_status_lb().text()

    @status.setter
    def status(self, value: str):
        self.get_status_lb().setText(value)

    def get_help_btn(self) -> QPushButton:
        return self.ui.pushButton_2

    def get_about_btn(self) -> QPushButton:
        return self.ui.pushButton_3

    def get_select_file_btn(self) -> QPushButton:
        return self.ui.pushButton

    def get_select_file_lineedit(self) -> QLineEdit:
        return self.ui.lineEdit

    def get_start_btn(self) -> QPushButton:
        return self.ui.pushButton_4

    def get_status_lb(self) -> QLabel:
        return self.ui.label_2

    def _set_file_path(self, path: str):
        if not path:
            return

        if not path.endswith(('.pmx', '.vmd')):
            loguru.logger.warning(f'文件必须是pmx或者vmd文件, 当前文件为{path}')
            self.status = ProgramsStatus.UnSelectFile.value
            self.get_select_file_lineedit().clear()
            return

        self.get_select_file_lineedit().setText(path)
        self.status = ProgramsStatus.SelectFile.value

    def _connect_signal(self):
        self.ui.pushButton_2.clicked.connect(self._show_help)
        self.ui.pushButton_3.clicked.connect(self._show_about)

    def _show_help(self):
        self._help_text_edit.setText(HELP)
        self._help_text_edit.show()

    def _show_about(self):
        self._about_text_edit.setText(ABOUT)
        self._about_text_edit.show()

    def dragEnterEvent(self, e: QDragEnterEvent):
        if e.mimeData().hasText():
            self._drop_mask.show()

    # 窗体大小发生改变触发事件
    def resizeEvent(self, event):
        self.resize_mask()

    def resize_mask(self) -> None:
        self._drop_mask.resize(self.width(), self.height())


if __name__ == '__main__':
    app = QApplication([])
    window = MainView()
    window.show()
    print('haha')
    print('这里是更多的测试')
    app.exec()
